package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import android.util.Log;

import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResOrderEntity;
import com.taomu.app.zongchouhuli.http.res.ResOrderListEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.OrderListContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.OrderEntity;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class OrderListPresenter implements OrderListContract.Presenter {

    private OrderListContract.View mContractView;

    private CompositeDisposable disposable;

    public OrderListPresenter(OrderListContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getOrderListData(String typeid,int ship,int take) {
        Request.getIntence().getOrderList(StorageHelper.getToken(),typeid,String.valueOf(ship),String.valueOf(take))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResOrderListEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResOrderListEntity resProductEntities) {
                        //Log.d("wqdqwdwqdwqdwq","onNext:"+resProductEntities.getData().size());
                        if(mContractView != null){
                            mContractView.setOrderListData(resProductEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });
    }

    @Override
    public void cancleOrder(String orderid) {
        Request.getIntence().cancleOrder(StorageHelper.getToken(),orderid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            mContractView.setCancleResults(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void deleteOrder(String orderid) {
        Request.getIntence().deleteOrder(orderid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            mContractView.setCancleResults(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void detch() {

    }

    private String[] types = {"待付款","审核中","已完成","待付款"};

    private void setOrderListData(String typeid){
        List<OrderEntity> mData = new ArrayList<>();
        if("1".equals(typeid)){
            for(int i=0;i<4;i++){
                OrderEntity entity = new OrderEntity();
                entity.setIcon("");
                entity.setOrderNum("278985544");
                entity.setState(types[i]);
                entity.setOrderid(String.valueOf(i));
                entity.setStateId(String.valueOf(i+1));
                entity.setPrice("100");
                mData.add(entity);
            }
        }else if("2".equals(typeid)){
            for(int i=0;i<5;i++){
                OrderEntity entity = new OrderEntity();
                entity.setIcon("");
                entity.setOrderid(String.valueOf(i));
                entity.setOrderNum("278985544");
                entity.setState("待付款");
                if(i%2 == 0) {
                    entity.setStateId("1");
                }else{
                    entity.setStateId("4");
                }
                entity.setPrice("100");
                mData.add(entity);
            }
        }else if("3".equals(typeid)){
            for(int i=0;i<3;i++){
                OrderEntity entity = new OrderEntity();
                entity.setIcon("");
                entity.setOrderNum("278985544");
                entity.setOrderid(String.valueOf(i));
                entity.setState("审核中");
                entity.setStateId("2");
                entity.setPrice("100");
                mData.add(entity);
            }
        }else if("4".equals(typeid)){
            for(int i=0;i<2;i++) {
                OrderEntity entity = new OrderEntity();
                entity.setIcon("");
                entity.setOrderid(String.valueOf(i));
                entity.setOrderNum("278985544");
                entity.setState("已完成");
                entity.setStateId("3");
                entity.setPrice("100");
                mData.add(entity);
            }
        }
        if(mContractView != null){
            //mContractView.setOrderListData(mData);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
